package gui;

import java.awt.Component;
import java.awt.Font;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import client.Main;
import client.ReportControl;

public class CompRepGUI extends JFrame{

	JPanel panel;
	
	JLabel lblHeader;
	
	JButton btnBack;
	JButton btnLogOut;
	
	JTable tbl;
	JScrollPane scrReport;
	
	Object[][] data;
	String[] colComp={"Report ID","Complaint ID","Date Received","Time Received","Compensation","Response Date","Response Time","Status","Report Date"};
	
	public CompRepGUI()
	{
		super();
		init();
	}
	
	JScrollPane getSatTable()
	{
		data=ReportControl.mngCompRep();
		DefaultTableModel model = new DefaultTableModel(data, colComp) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		      return false;
		      }
		  };
		  tbl=new JTable(model);
		  
		  TableColumn column = tbl.getColumnModel().getColumn(7);
		  column.setPreferredWidth(0);
		  column.setCellEditor(new DefaultCellEditor(new JCheckBox()));
		  tbl.getColumnModel().getColumn(7).setCellRenderer(
					new TableCellRenderer() {
	                            // the method gives the component  like whome the cell must be rendered
	                            public Component getTableCellRendererComponent(
								JTable table, Object value, boolean isSelected,
								boolean isFocused, int row, int col) {
							boolean marked = (Boolean) value;
							JCheckBox rendererComponent = new JCheckBox();
							if (marked) {
								rendererComponent.setSelected(true);
							}
							return rendererComponent;
						}
					});
		  
		  scrReport=new JScrollPane(tbl);
		  scrReport.setBounds(90, 120, 800, 140);
		  return scrReport;
	}
	
	JButton getButtonBack()
	{
		if(btnBack==null)
		{
			btnBack=new JButton();
			btnBack.setText("Back");
			btnBack.setBounds(880, 20, 100, 40);
		}
		return btnBack;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Complaints Report");
			lblHeader.setBounds(390, 60, 220, 30);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
JButton getBtnLogOut() {
		
		if (btnLogOut == null){
			btnLogOut = new JButton();
			btnLogOut.setBounds(20,20,100,40);
			btnLogOut.setText("LogOut");
			btnLogOut.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
					{
						dispose();
						Main.main.logOut.doClick();
					}
				}
			});
		}
		return btnLogOut;
	}
	
	JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(this.getWidth(),this.getHeight());
			panel.setLayout(null);
			panel.add(getLabelHeader());
			panel.add(getBtnLogOut());
			panel.add(getButtonBack());
			panel.add(getSatTable());
			panel.setSize(1000, 400);
			//panel.add(lblMonth, null);
			
		}
		return panel;
	}
	
	void init()
	{
		this.setBounds((int)Main.dim.getWidth()/2-500, (int)Main.dim.getHeight()/2-275, 1000, 400);
		this.setTitle("Zer Lah - Complaints Report");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
	}
}
